package com.sphd.Restaurant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sphd.Restaurant.R;
import com.sphd.Restaurant.adapters.ScoreAdapter;
import com.sphd.Restaurant.base.BaseActivity;
import com.sphd.Restaurant.infos.IntegralInfo;
import com.sphd.Restaurant.infos.RestaurantInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.ScoresP;
import com.sphd.Restaurant.utils.AppManager;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.ScoresView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScoresActivity extends BaseActivity implements ScoresView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_score)
    TextView myScore;
    @BindView(R.id.look_my_record)
    TextView lookMyRecord;
    @BindView(R.id.my_score_layout)
    LinearLayout myScoreLayout;
    @BindView(R.id.my_listView)
    ListView myListView;
    Intent intent;
    Bundle bundle;
    List<IntegralInfo> integralInfos;
    ScoreAdapter scoreAdapter;
    RestaurantInfo restaurantInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        integralInfos = (List<IntegralInfo>) bundle.getSerializable(Constants.INTEGRAL);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constants.RESTAURANT);
        setToolBar(toolbar, "积分商品", true);
        scoreAdapter = new ScoreAdapter(integralInfos);
        myListView.setAdapter(scoreAdapter);
        if (getBoolean(Constants.ISLOGIN)) {
            myScoreLayout.setVisibility(View.VISIBLE);
            initSocore();
        } else {
            myScoreLayout.setVisibility(View.GONE);
        }
        toolbar.setNavigationOnClickListener(view -> AppManager.getAppManager().finishActivity(this));
    }

    private void initSocore() {
        ScoresP scoresP = new ScoresP(this);
        scoresP.addData(Model.HTTPURL + Model.ISVIP + "username=" + getString(Constants.USERPHONE) +
                "&cgid=" + restaurantInfo.getId());
    }


    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
        dissMissDialog();
    }

    @Override
    public void sendList(String mem_integral) {
        myScore.setText(mem_integral);
    }


    @OnClick(R.id.my_score_layout)
    public void onClick() {
        bundle.putString(Constants.TAG,ScoresActivity.class.getName());
        startActivity(IntegralActivity.class, bundle);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        AppManager.getAppManager().finishActivity(this);
    }
}
